package com.globalwave.common
{
	import com.globalwave.common.virtualmodule.VirtualDataGrid;
	import com.globalwave.util.HttpServiceUtil;
	import com.globalwave.util.U;
	
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import mx.controls.Alert;
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class SearchTextInput extends TextInput
	{
		public var code:String = null ;
		public var url:String = null ;
		
		public var valueField:String = "event_type_code" ;
		public var displayField:String = "name_" ;
		
		public var minLength:int = 2 ;
		
		private var _value:String = null ;
		
		private var listGrid:VirtualDataGrid = null ;
		private var dataService:HTTPService ;
		
		public function SearchTextInput()
		{
			super();
			
			this.listGrid = new VirtualDataGrid() ;
			//this.listGrid.init();
			this.listGrid.width = 300 ;
			this.listGrid.height = 400 ;
			
			this.addEventListener(FlexEvent.ENTER, thisOnEnter) ;
			this.addEventListener(FlexEvent.INITIALIZE, thisOnInit) ;
			
			//this.addEventListener(FocusEvent.FOCUS_OUT, uiOnFocusOut) ;
			
			this.listGrid.addEventListener(FocusEvent.FOCUS_OUT, uiOnFocusOut) ;
			this.listGrid.addEventListener(KeyboardEvent.KEY_DOWN, gridOnEnter) ;
		}
		
		private function uiOnFocusOut(event:FocusEvent):void {
			PopUpManager.removePopUp(this.listGrid) ;
		}
		
		private function thisOnEnter(event:FlexEvent):void  {
			
			if (this.text.length < this.minLength) {
				Alert.show("输入不正确，请最少输入" + this.minLength + "位字符！");
				return ;
			}
			
			var pt:Point = new Point(0, this.y);
			pt = this.localToGlobal(pt);
			
			this.listGrid.x = pt.x ;
			this.listGrid.y = pt.y ;
			this.listGrid.setFocus() ;
			
			PopUpManager.addPopUp(this.listGrid, this, false);
			
			doSearch() ;
		}
		
		private function gridOnEnter(event:KeyboardEvent):void  {
			if (event.charCode == Keyboard.ENTER) {
				if (this.listGrid.selectedItem != null) {
					this._value = U.obj2String(this.listGrid.selectedItem[this.valueField], "") ;
					this.text = U.obj2String(this.listGrid.selectedItem[this.displayField], "") ;
					PopUpManager.removePopUp(this.listGrid) ;
				}
			} else if (event.charCode == Keyboard.ESCAPE) {
				PopUpManager.removePopUp(this.listGrid) ;
			}
		}
		
		private function thisOnInit(event:FlexEvent):void  {
			getUIMetaData() ;
		}
		
		public function doSearch() : void {
			listGrid.criterion = new Object() ;
			//listGrid.criterion["view_id"] = "-6" ;
			listGrid.criterion["view_code"] = code ;
			listGrid.criterion["size"] = 500 ;
			
			listGrid.criterion["criterion.text"] = this.text ;
			
			listGrid.doRefresh() ;
		}
		
		public function getUIMetaData() : void { 
			var criterion:Object = new Object() ;  
			//criterion["view_id"] = "-6" ;
			criterion["view_code"] = code ;
			
			dataService = HttpServiceUtil.getService("/common/virtualModule_getUIMetaData") ;
			dataService.addEventListener(ResultEvent.RESULT, getUIMetaDataOnReturn) ;
			dataService.send(criterion) ;
		}
		
		private function getUIMetaDataOnReturn(event:ResultEvent) : void  {             
			if (event.result != null ) {
				this.listGrid.fields = new XML(event.result).grid.fields
				//this.metaData = new XML(event.result) ;
				
				//initSearchForm(metaData.searchForm.fields) ;
				
				//editFormWindow.fields = metaData.editForm.fields ;
				//editFormWindow.view = (metaData.editForm.view as XMLList)[0] ;
				
			}
		}
		
		public function get value():String {
		    return this._value ;
		}
		public function set value(v:String):void {
		    this._value = v ;
		}
	}
}